package com.test.test

import android.annotation.SuppressLint
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.test.test.databinding.FragmentSecondBinding
/**
 * 这个是有一个Rv的Fragment
 */
class SecondFragment: Fragment() {
    private lateinit var mBinding: FragmentSecondBinding
    private var itemData: MutableList<String> = mutableListOf()
    private var startY: Float = 0f
    //向外部提供滑动参数接口
    private var onRvScrollListener: ((scrollY: Float) -> Unit)? = null

    fun setOnRvScrollListener(onRvScrollListener: ((scrollY: Float) -> Unit)?){
        this.onRvScrollListener = onRvScrollListener
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        for (i in 0 .. 20){
            itemData.add(i,i.toString())
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        mBinding = FragmentSecondBinding.inflate(inflater,container,false)
        initView()
        initEvent()
        return mBinding.root
    }

    private fun initView(){
        mBinding.apply {
            rvTest.apply {
                layoutManager = RvLayoutManager(requireContext())
                adapter = RvAdapter(itemData)
            }
        }

    }
    @SuppressLint("ClickableViewAccessibility")
    private fun initEvent(){
        mBinding.apply {
            rvTest.setOnTouchListener { _, motionEvent ->
                motionEvent.apply {
                    when(action){
                        MotionEvent.ACTION_DOWN -> {
                            startY = rawY
                        }
                        MotionEvent.ACTION_MOVE -> {
                            //将滑动距离传出，供外界响应
                            onRvScrollListener?.invoke(rawY - startY)
                            startY = rawY
                        }
                        MotionEvent.ACTION_CANCEL -> {

                        }
                        MotionEvent.ACTION_UP -> {

                        }
                    }
                }
                false
            }


        }
    }

    inner class RvAdapter(itemData: MutableList<String>): BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_test, itemData){
        override fun convert(holder: BaseViewHolder, item: String) {
            holder.setText(R.id.tv_item,"这是第" + item + "个Item")
        }
    }

    /**
     * 用于设置选项列表是否可滚动的Manager(item少于maxCount时不生效，保持不可滑动)
     */
    inner class RvLayoutManager(context: Context?) : LinearLayoutManager(context) {
        //是否可垂直滑动
        var isScrollVertically = true
        //最大显示数量
        var maxCount = 8
        /**
         * 基于子View高度及限制个数获取Rv最大显示高度
         */
        private val maxHeight: Int
            get() {
                if(childCount == 0 || maxCount <= 0) return 0
                val child = getChildAt(0)
                val childParams = child?.layoutParams as RecyclerView.LayoutParams
                var childHeight = child.height
                childHeight += childParams.topMargin + childParams.bottomMargin
                return childHeight * maxCount + paddingTop + paddingBottom
            }

        override fun setMeasuredDimension(widthSize: Int, heightSize: Int) {
            if(maxHeight > 0 && childCount > maxCount)
                super.setMeasuredDimension(widthSize, maxHeight)
            else
                super.setMeasuredDimension(widthSize, heightSize)
        }

        override fun canScrollVertically(): Boolean {
            return isScrollVertically && super.canScrollVertically() && itemData.size > maxCount
        }

    }

}